home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / C Internet Config / IC Application Source ƒ / 68k Internet Config ƒ / C Source ƒ / IC Text Whats.c < prev    next >
Encoding:
Text File  |  1995-12-01  |  11.0 KB  |  281 lines  |  [TEXT/SPM ]

  1. /*
  2.     IC Text Whats.c
  3.     
  4. */
  5.  
  6. #include <Scrap.h>
  7.  
  8. #include "IC Globals.h"
  9.  
  10. #include "IC Window Globals.h"
  11. #include "IC Types.h"
  12. #include "IC API.h"
  13. #include "IC Globals.h"
  14. #include "IC Misc Subs.h"
  15. #include "IC Subs.h"
  16. #include "IC Text.h"
  17. #include "IC Dialogs.h"
  18. #include "IC Document.h"
  19. #include "IC Text Whats.h"
  20.  
  21. pascal void DrawTextProc(DialogPtr window,short item){
  22.     Rect r;
  23.     short savefont,savemode,savesize;
  24.     Style saveface;
  25.     
  26.     savefont=window->txFont;
  27.     saveface=window->txFace;
  28.     savemode=window->txMode;
  29.     savesize=window->txSize;
  30.     
  31.     TextDraw(WindowInfo[GetWindowType(window)].items[item]->data);
  32.     
  33.     GetDItemRect(window,item,&r);
  34.     InsetRect(&r,-3,-3);
  35.     PenNormal();
  36.     FrameRect(&r);
  37.     
  38.     TextFont(savefont);
  39.     TextFace(saveface);
  40.     TextMode(savemode);
  41.     TextSize(savesize);
  42. }
  43.  
  44. Boolean HaveTextSelection(WindowType wt){
  45.     long start_sel,end_sel;
  46.     Boolean ret=false;
  47.     
  48.     if (GetSelectedItem(wt)>0){
  49.         TextGetSelect(WindowInfo[wt].items[GetSelectedItem(wt)]->data,&start_sel,&end_sel);
  50.         ret=(start_sel!=end_sel);
  51.     }
  52.     
  53.     return ret;
  54. }
  55.  
  56. void AdjustTextMenu(WindowType wt){
  57.     Boolean enable_cut=false,enable_paste=false,enable_select_all=false;
  58.     Boolean enable_undo=false;
  59.     long offset;
  60.     MenuHandle mh;
  61.     Handle texth;
  62.     long text_size;
  63.     short search_text;
  64.     long cr_pos;
  65.     short item;
  66.     OSErr err;
  67.     long sel_start,sel_end;
  68.     
  69.     item=GetSelectedItem(wt);
  70.     if ((wt!=WT_None)&&(item>0)){
  71.         enable_cut=HaveTextSelection(wt);
  72.         offset=0;
  73.         enable_paste=(GetScrap((Handle)0,'TEXT',&offset)>0);
  74.         TextGetSelect(WindowInfo[wt].items[item]->data,&sel_start,&sel_end);
  75.         TextGetSize(WindowInfo[wt].items[item]->data,&text_size);
  76.         enable_select_all=((sel_start!=0)||(sel_end!=text_size));
  77.     }
  78.     
  79.     // deal with the nasty paste problem, ie preventing them pasting CRs into single line fields
  80.     if ((enable_paste)&&(WindowInfo[wt].items[item]->flags&(1<<tf_single_line))){
  81.         texth=NewHandle(0);
  82.         err=MemError();
  83.         if (err==noErr){
  84.             offset=0;
  85.             text_size=GetScrap(texth,'TEXT',&offset);
  86.             if (text_size>0){
  87.                 search_text=0x0d0d;
  88.                 cr_pos=Munger(texth,0,(Ptr)&search_text,1,    }
  89. }
  90.  
  91. OSErr WhatOpenText(WindowType wt,short item){
  92.     OSErr err;
  93.     Str31 key;
  94.     long attr;
  95.     Handle texth;
  96.     long junk,flags;
  97.     Boolean pstring,scrambled;
  98.     short font,size;
  99.     Rect r;
  100.     
  101.     WindowInfo[wt].items[item]->data=(Ptr)0;
  102.     WindowInfo[wt].items[item]->spare_data=(Ptr)0;
  103.     texth=(Handle)0;
  104.     
  105.     SetPString(key,1,WindowInfo[wt].items[item]->key);
  106.     flags=WindowInfo[wt].items[item]->flags;
  107.     pstring=flags&(1<<tf_pstring);
  108.     scrambled=flags&(1<<tf_scrambled);
  109.     
  110.     if (flags&(1<<tf_monospace)){
  111.         font=monaco;
  112.         size=9;
  113.     } else {
  114.         font=systemFont;
  115.         size=12;
  116.     }
  117.     
  118.     err=ICMapErr(ICGetPrefHandle(GetInstance(),key,&attr,&texth));
  119.     if (err==noErr){
  120.         ProcessAttributes(wt,item,attr);
  121.         if (pstring)
  122.             Munger(texth,0,(Ptr)0,1,(Ptr)&junk,0);// strip the first char
  123.         if (scrambled)
  124.             ScrambleHandle(texth);
  125.         err=TextCreate(&(WindowInfo[wt].items[item]->data),WindowInfo[wt].window,item,font,
  126.             size,IsLocked(wt,item));
  127.     }
  128.     if (err==noErr){
  129.         SetDItemHandle(WindowInfo[wt].window,item,(Handle)gDrawTextProc);
  130.         junk=GetHandleSize(texth);
  131.         // (*texth)[junk]=0;
  132.         TextInsert(WindowInfo[wt].items[item]->data,texth);
  133.     }
  134.     
  135.     // build password dialog string
  136.     if ((err==noErr)&&(scrambled)){
  137.         WindowInfo[wt].items[item]->spare_data=WindowInfo[wt].items[item]->data;
  138.         WindowInfo[wt].items[item]->data=(Ptr)0;
  139.         GetDItemRect(WindowInfo[wt].window,item,&r);
  140.         OffsetRect(&r,16000,0);
  141.         TextMove(WindowInfo[wt].items[item]->spare_data,&r);
  142.         BlockFill(*texth,GetHandleSize(texth),'•');
  143.         
  144.         err=TextCreate(&(WindowInfo[wt].items[item]->data),WindowInfo[wt].window,item,font,
  145.             size,IsLocked(wt,item));
  146.         
  147.         if (err==noErr)
  148.             TextInsert(WindowInfo[wt].items[item]->data,texth);
  149.     }
  150.     
  151.     if (texth!=(Handle)0)
  152.         DisposeHandle(texth);
  153.     
  154.     return err;
  155. }
  156.  
  157. OSErr WhatFlushText(WindowType wt,short item){
  158.     OSErr err;
  159.     Str31 key;
  160.     Handle texth=(Handle)0,oldtexth=(Handle)0;
  161.     long attr,flags,texts,oldtexts;
  162.     short i;
  163.     Boolean pstring,scrambled;
  164.     
  165.     texth=oldtexth=(Handle)0;
  166.     SetPString(key,1,WindowInfo[wt].items[item]->key);
  167.     flags=WindowInfo[wt].items[item]->flags;
  168.     pstring=flags&(1<<tf_pstring);
  169.     scrambled=flags&(1<<tf_scrambled);
  170.     err=ICMapErr(ICGetPrefHandle(GetInstance(),key,&attr,&oldtexth));
  171.     if (err==noErr){
  172.         if ((pstring)&&(GetHandleSize(oldtexth)==0)){
  173.             // pref is non-existent or empty, turn oldtext into a handle for an empty pascal string
  174.             SetHandleSize(oldtexth,1);
  175.             err=MemError();
  176.             if (err==noErr)
  177.                 **oldtexth=0; // terminate the empty pstring
  178.         }
  179.     }
  180.     if (err==noErr){
  181.         texth=NewHandle(0);
  182.         err=MemError();
  183.     }
  184.     
  185.     if (err==noErr){
  186.         if (scrambled){
  187.             TextGet(WindowInfo[wt].items[item]->spare_data,texth);
  188.             ScrambleHandle(texth);
  189.         } else
  190.             TextGet(WindowInfo[wt].items[item]->data,texth);
  191.         if (pstring){
  192.             if (GetHandleSize(texth)>255)
  193.                 SetHandleSize(texth,255);
  194.             i=GetHandleSize(texth)*0x0101; // puts it into both bytes!
  195.             // insert the size byte into the handle at position zero
  196.             Munger(texth,0L,(Ptr)0,0L,&i,1);// if this errors, we lose data
  197.         }
  198.         // should it go out to the prefs file?
  199.         texts=GetHandleSize(texth);
  200.         oldtexts=GetHandleSize(oldtexth);
  201.         {
  202.             Boolean islocked=IsLocked(wt,item);
  203.             Boolean sameSize=(texts==oldtexts);
  204.             Boolean sameText=true;
  205.             
  206.             if (sameSize) // only check the text if the sizes are the same
  207.                 sameText=BlockCompare(*texth,*oldtexth,texts);
  208.             
  209.             if (islocked){
  210.                 // can't update the doc, it is a locked field
  211.             } else if (sameSize&&sameText){
  212.                 // nothing changed, so don't update the doc
  213.             } else {
  214.                 // either the size or text is different, update the doc
  215.                 
  216.                 err=ICMapErr(ICSetPrefHandle(GetInstance(),key,ICattr_no_change,texth));
  217.                 
  218.                 if (err==noErr)
  219.                     DirtyDocument();
  220.             }
  221.         }
  222.     }
  223.     if (texth!=(Handle)0)
  224.         DisposeHandle(texth);
  225.     if (oldtexth!=(Handle)0)
  226.         DisposeHandle(oldtexth);
  227.     
  228.     return err;
  229. }
  230.  
  231. OSErr WhatCloseText(WindowType wt,short item){
  232.     long flags;
  233.     Boolean scrambled;
  234.     
  235.     flags=WindowInfo[wt].items[item]->flags;
  236.     scrambled=flags&(1<<tf_scrambled);
  237.     TextDestroy(&(WindowInfo[wt].items[item]->data));
  238.     if (scrambled)
  239.         TextDestroy(&(WindowInfo[wt].items[item]->spare_data));
  240.     return noErr;
  241. }
  242.  
  243. OSErr WhatClickText(WindowType wt,short item,EventRecord* er){
  244.     JustSelectTextItem(wt,item);
  245.     TextClick(WindowInfo[wt].items[item]->data,er);
  246.     return noErr;
  247. }
  248.  
  249. OSErr WhatKeyText(WindowType wt,short item,EventRecord* er){
  250.     long flags;
  251.     Boolean scrambled,single_line;
  252.     char ch;
  253.     long sel_start,sel_end;
  254.     
  255.     flags=WindowInfo[wt].items[item]->flags;
  256.     scrambled=flags&(1<<tf_scrambled);
  257.     single_line=flags&(1<<tf_single_line);
  258.     ch=(er->message&charCodeMask)&0xff;
  259.     
  260.     // if a return on a single line or whitespace with scrambled
  261.     if (((single_line)&&(ch==13))||((scrambled)&&(ch!=8)&&(ch<' ')))
  262.         SysBeep(1);
  263.     else {
  264.         if ((IsLocked(wt,item))&&(DirtyKey(ch)))
  265.             LockedAlert(wt,item);
  266.         else {
  267.             if (scrambled){
  268.                 TextGetSelect(WindowInfo[wt].items[item]->data,&sel_start,&sel_end);
  269.                 TextSetSelect(WindowInfo[wt].items[item]->spare_data,sel_start,sel_end);
  270.                 TextKey(WindowInfo[wt].items[item]->spare_data,er);
  271.                 if ((ch!=8)&&(DirtyKey(ch)))
  272.                     er->message='•';
  273.             }
  274.             
  275.             TextKey(WindowInfo[wt].items[item]->data,er);
  276.         }
  277.     }
  278.     
  279.     return noErr;
  280. }
  281.